﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business.Project;
using AMM.Business.Store;
using AMM.Model;
using AMM.Model.Project;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class MouldShiftSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int mouldValue = Convert.ToInt32(this.ddlMould.SelectedValueEx);
            int fromVendorValue = Convert.ToInt32(this.ddlFromVendor.SelectedValueEx);
            int toVendorValue = Convert.ToInt32(this.ddlToVendor.SelectedValueEx);

            MouldShiftSearchModel searchResult = new MouldShiftSearchModel();
            searchResult.Code = this.tbCode.Text;
            searchResult.MouldId = mouldValue;
            searchResult.FromVendorId = fromVendorValue;
            searchResult.ToVendorId = toVendorValue;
            searchResult.ShiftDateStart = this.GetDateTimePickerValue(this.dtpShiftDateStart);
            searchResult.ShiftDateEnd = this.GetDateTimePickerValue(this.dtpShiftDateEnd);

            if (mouldValue == 0) { searchResult.MouldId = null; }
            if (fromVendorValue == 0) { searchResult.FromVendorId = null; }
            if (toVendorValue == 0) { searchResult.ToVendorId = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.tbCode.Clear();
            this.dtpShiftDateStart.Checked = false;
            this.dtpShiftDateEnd.Checked = false;
            this.ddlMould.SelectedValueEx = null;
            this.ddlFromVendor.SelectedValueEx = null;
            this.ddlToVendor.SelectedValueEx = null;
        }

        public MouldShiftSearchControl() {
            this.InitializeComponent();
        }

        private void BindFromVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel).ToList();

            this.ddlFromVendor.DisplayMemberEx = "Code";
            this.ddlFromVendor.ValueMemberEx = "Key";
            this.ddlFromVendor.HeightEx = 300;
            this.ddlFromVendor.WidthEx = 400;
            this.ddlFromVendor.PageSizeEx = 20;
            this.ddlFromVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }

        private void BindToVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel).ToList();

            this.ddlToVendor.DisplayMemberEx = "Code";
            this.ddlToVendor.ValueMemberEx = "Key";
            this.ddlToVendor.HeightEx = 300;
            this.ddlToVendor.WidthEx = 400;
            this.ddlToVendor.PageSizeEx = 20;
            this.ddlToVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }

        private void BindMould() {
            MouldSearchModel searchModel = new MouldSearchModel();
            MouldProcessor mouldProcessor = new MouldProcessor(int.MaxValue);
            List<MouldViewModel> mouldList = mouldProcessor.FetchMouldViewModelList(searchModel).ToList();

            this.ddlMould.DisplayMemberEx = "Code";
            this.ddlMould.ValueMemberEx = "Key";
            this.ddlMould.HeightEx = 300;
            this.ddlMould.WidthEx = 400;
            this.ddlMould.PageSizeEx = 20;
            this.ddlMould.DataBind(new MouldViewModel(), x => new { x.Code, x.Name, x.MouldType }, mouldList);
        }

        private void MouldShiftSearchControl_Load(object sender, EventArgs e) {
            this.BindFromVendor();
            this.BindToVendor();
            this.BindMould();
        }
    }
}
